package com.zmn.oms.task.job.order;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.order.DuplicateOrderBService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.dto.work.modify.must.ZsCancelDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalWorkBService;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@Slf4j
@LTS
public class OrderJob {

    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    ZsNormalWorkBService zsNormalWorkBService;
    @Autowired
    DuplicateOrderBService duplicateOrderBService;

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_PREPAY_TIMEOUT_CANCEL)
    public Result prepayTimeout(Job job) {

        log.info("#order#prepaytimeout 执行：" + job);

        String orderIdStr = job.getParam("orderId");
        if (!NumberUtil.isNumber(orderIdStr)) {
            return new Result(Action.EXECUTE_FAILED, "订单号错误!");
        }

        Long orderId = Long.valueOf(orderIdStr);
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, orderId);
        if (orderWork == null) {
            log.error("#order#prepaytimeout 订单不存在");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        Integer prepayAmount = orderWork.getPrepayAmount();
        if (NumberUtil.isNullOrZero(prepayAmount)) {
            log.info("#order#prepaytimeout 没有啄木鸟预付款 无需自动取消");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        Integer prepayStatus = orderWork.getPrepayStatus();
        if (Objects.equals(prepayStatus, PayConsts.PAY_STATUS_DONE)) {
            log.info("#order#prepaytimeout 预付款已经支付无需取消");
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }

        ZsCancelDTO zsCancelDTO = new ZsCancelDTO();
        zsCancelDTO.setOrderId(orderId);
        zsCancelDTO.setWorkId(orderId);
        zsCancelDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
        zsCancelDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
        zsCancelDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
        zsCancelDTO.setOperatorRemark("超时取消");

        try {
            zsNormalWorkBService.saveCancel(zsCancelDTO);
        } catch (OmsBaseException e) {
            log.error("#order#prepaytimeout 取消异常 {} {}", e.getMessage(), e);
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
        }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
